<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="formVisible" append-to-body width="500px">
      <el-form ref="myForm" :model="form" label-width="100px">
        <el-form-item label="权限字符" required prop="name">
          <el-input v-model="form.name" placeholder="权限字符" />
        </el-form-item>
        <el-form-item label="权限名称" required prop="description">
          <el-input v-model="form.description" placeholder="权限名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </div>
    </el-dialog>

    <el-form :inline="true" size="small">
      <el-form-item label="搜索:">
        <el-input v-model="searchParams.search" placeholder="请输入搜索内容" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-circle-plus" @click="handleInsert">添加权限</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="center" label="权限字符">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column align="center" label="权限名称">
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div :style="{textAlign:'center',padding:'10px'}">
      <el-pagination
        background
        :current-page.sync="searchParams.pageNum"
        :page-sizes="[10, 20, 30, 40,50]"
        :page-size.sync="searchParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchParams.total"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
  </div>
</template>

<script>
import {
  permissionList,
  delPermission,
  savePermission
} from '@/api/system/permission'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      formVisible: false,
      form: {},
      searchParams: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
        search: ''
      },
      title: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleUpdate(row) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs['myForm'].resetFields()
        this.title = '修改权限信息'
        this.form = { ...row }
      })
    },
    handleInsert() {
      this.title = '添加权限信息'
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs['myForm'].resetFields()
        this.form = {}
      })
    },
    handleSubmit() {
      this.$refs['myForm'].validate(valid => {
        if (valid) {
          savePermission(this.form).then(() => {
            this.$message.success('保存信息成功')
            this.fetchData()
            this.formVisible = false
          })
        }
      })
    },
    handleDelete({ id }) {
      this.$confirm('确认删除此信息吗？', '警告', {
        cancelButtonText: '取消',
        confirmButtonText: '确认'
      }).then(() => {
        delPermission(id).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    fetchData() {
      this.listLoading = true
      permissionList(this.searchParams).then(response => {
        const { records, total } = response.data
        this.list = records
        this.searchParams.total = total
        this.listLoading = false
      })
    }
  }
}
</script>

